<?php
/*
 * This file is part of the LynxCms .
 * (c) 2009-2010 Henry Vallenilla <henryvallenilla@gmail.com>
 */

/**
 * uploadFileHelper. represents a resized uploaded file.
 *
 * @package    symfony
 * @subpackage helper
 * @author     Henry Vallenilla <henryvallenilla@gmail.com>
 * @version    SVN: $Id: uploadFileHelper.php 1.0
 */

/**
 * Saves to uploaded file
 *
 * This method can throw exceptions if there is a problem when saving the file.
 *
 * If you don't pass a file name, it will be generated by the generateFilename method.
 * This will only work if you have passed a path when initializing this instance.
 *
 * @param <string> $fileOriginal       The original file name
 * @param <string> $tempFileName       The absolute temporary path to the file
 * @param <int> $delete
 * @param <string> $dirUploadFile      The path to save the file (optional).
 * @param <int> $idReference           Indicates the unique id to concatenate the file name
 * @param <int> $inModule           Indicates the  file of configuration the upload images
 * @param <bool> $create
 * @param <int> $dirMode
 * @return <string> The filename
 *
 * @throws Exception
 */
function loadFiles($fileOriginal, $tempFileName , $delete, $dirUploadFile, $idReference = 0, $inModule = true, $create = true, $dirMode = 0777)
{
    // Instancio LyxnValida
    
    //$lxValida = new validations();
    $fileName = $fileOriginal;
    //$fileName = $lxValida->preparaArchivo($fileName,20);        
    $fileName = $idReference."_".$fileName;    
    // Check if have the permissions
    
    if (!is_readable($dirUploadFile))
    {
        if ($create && !mkdir($dirUploadFile, $dirMode, true))
        {
            // failed to create the directory
            throw new Exception(sprintf('Failed to create file upload directory "%s".', $dirUploadFile));
        }
        // chmod the directory since it doesn't seem to work on recursive paths
        chmod($dirUploadFile, $dirMode);
    }
    // Check if directory exist
    if (!is_dir($dirUploadFile))
    {
        // the directory path exists but it's not a directory
        throw new Exception(sprintf('File upload path "%s" exists, but is not a directory.', $dirUploadFile));
    }
    // Check if the directory isn't writable
    if (!is_writable($dirUploadFile))
    {
        // the directory isn't writable
        throw new Exception(sprintf('File upload path "%s" is not writable.', $dirUploadFile));
    }
    
    if($inModule)
    {
        // The configuration of upload image it is module.yml
        $configParam = sfConfig::get('mod_'.strtolower(sfContext::getInstance()->getModuleName()).'_configuration_upload_images');
    }else{
        // The configuration of upload image it is app.yml
        $configParam = sfConfig::get('app_upload_images_'.strtolower(sfContext::getInstance()->getModuleName()));
    }
    
    // Creates the images according to the number of copies specified in the YAML    
    for($i=1;$i <= $configParam['copies'];$i++)
    {
        $thumbnail = new sfThumbnail($configParam['size_'.$i]['image_width_'.$i], $configParam['size_'.$i]['image_height_'.$i],true, false, 100, 'sfGDAdapter');
        $thumbnail->loadFile($tempFileName);
        // Dinamic FilePath
        $filepath = $dirUploadFile.$configParam['size_'.$i]['pref_'.$i].'_'.$fileName;
        $thumbnail->save($filepath, 'image/jpeg');
        // chmod our file
        chmod($filepath, 0777);
    }
    // Return the name of the file that was built
    return $fileName;
}


